package com.gzdh.dishrecognition.ui.fragment.sysset;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Toast;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import com.blankj.utilcode.util.StringUtils;
import com.gzdh.dishrecognition.R;
import com.gzdh.dishrecognition.config.IdfConst;
import com.gzdh.dishrecognition.ui.dialog.InputDialog;
import com.gzdh.dishrecognition.ui.dialog.identify.LoginFragment;
import com.gzdh.dishrecognition.ui.dialog.identify.RegIdentifyDialog;
import com.gzdh.dishrecognition.ui.view.OptionItemView;
import com.gzdh.dishrecognition.util.SdcardUtils;

import java.util.Objects;

/**
 * 菜品识别设置
 */
public class IdentifyBaseSetFragment extends Fragment {
    private Context mContext;
    private final Activity mActivity;


    private OptionItemView regView;

    public IdentifyBaseSetFragment(Activity activity) {
        this.mActivity = activity;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mContext = inflater.getContext();
        View view = getViews();
        init();
        return view;
    }

    private View getViews() {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        LinearLayout mainLayout = new LinearLayout(mContext);
        mainLayout.setLayoutParams(layoutParams);
        mainLayout.setBackgroundColor(Color.WHITE);
        mainLayout.setOrientation(LinearLayout.VERTICAL);
        mainLayout.addView(getBody());        //主题内容
        return mainLayout;
    }


    @SuppressLint({"UseCompatLoadingForDrawables", "SetTextI18n"})
    private View getBody() {
        ScrollView scrollView = new ScrollView(mContext);
        scrollView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        StateListDrawable selector = new StateListDrawable();

        selector.addState(new int[]{android.R.attr.state_pressed},ContextCompat.getDrawable(mContext,R.color.gray8));
        selector.addState(new int[]{android.R.attr.state_selected}, ContextCompat.getDrawable(mContext,R.color.white));
        selector.addState(new int[]{}, ContextCompat.getDrawable(mContext,R.color.white));


        LinearLayout mainLinear = new LinearLayout(mContext);
        mainLinear.setOrientation(LinearLayout.VERTICAL);
        scrollView.addView(mainLinear);

        regView = new OptionItemView(mContext);
        LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 55);
        regView.setLayoutParams(linearParams);
        regView.setPadding(20, 20, 20, 20);
        regView.setBackground(selector);
        regView.setLeftImageMarginLeft(20);
        regView.setLeftImageMarginRight(18);
        regView.setLeftImage(drawableToBitmap(Objects.requireNonNull(ContextCompat.getDrawable(mContext, R.mipmap.setting_icon))));
        regView.setLeftText("系统激活");
        regView.setLeftTextSize(16);
        regView.setLeftTextColor(Color.BLACK);
        regView.setRightImage(drawableToBitmap(Objects.requireNonNull(ContextCompat.getDrawable(mContext, R.mipmap.enter_icon))));
        regView.setOnClickListener(view -> {
            RegIdentifyDialog regIdentifyDialog = new RegIdentifyDialog(mActivity);
            regIdentifyDialog.show();
        });
        mainLinear.addView(regView);

        View line = new View(mContext);
        line.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1));
        line.setBackgroundColor(Color.rgb(224, 224, 224));
        mainLinear.addView(line);

        OptionItemView loginView = new OptionItemView(mContext);
        loginView.setPadding(20, 20, 20, 20);
        loginView.setLayoutParams(linearParams);
        selector = new StateListDrawable();
        selector.addState(new int[]{android.R.attr.state_pressed}, ContextCompat.getDrawable(mContext,R.color.gray8));
        selector.addState(new int[]{android.R.attr.state_selected}, ContextCompat.getDrawable(mContext,R.color.white));
        selector.addState(new int[]{}, ContextCompat.getDrawable(mContext,R.color.white));
        loginView.setBackground(selector);
        loginView.setLeftImageMarginLeft(20);
        loginView.setLeftImageMarginRight(18);
        loginView.setLeftImage(drawableToBitmap(Objects.requireNonNull(ContextCompat.getDrawable(mContext, R.mipmap.setting_icon))));
        loginView.setLeftText("识别账号");
        loginView.setLeftTextSize(16);
        loginView.setLeftTextColor(Color.BLACK);
        loginView.setRightImage(drawableToBitmap(Objects.requireNonNull(ContextCompat.getDrawable(mContext, R.mipmap.enter_icon))));
        loginView.setOnClickListener(view -> {
            LoginFragment loginFragment = new LoginFragment();
            loginFragment.show(getChildFragmentManager(), LoginFragment.class.getSimpleName());
        });
        mainLinear.addView(loginView);

        line = new View(mContext);
        line.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1));
        line.setBackgroundColor(Color.rgb(224, 224, 224));
        mainLinear.addView(line);

        OptionItemView idfServiceView = new OptionItemView(mContext);
        idfServiceView.setPadding(20, 20, 20, 20);
        idfServiceView.setLayoutParams(linearParams);
        selector = new StateListDrawable();
        selector.addState(new int[]{android.R.attr.state_pressed}, ContextCompat.getDrawable(mContext,R.color.gray8));
        selector.addState(new int[]{android.R.attr.state_selected}, ContextCompat.getDrawable(mContext,R.color.white));
        selector.addState(new int[]{}, ContextCompat.getDrawable(mContext,R.color.white));
        idfServiceView.setBackground(selector);
        idfServiceView.setLeftImageMarginLeft(20);
        idfServiceView.setLeftImageMarginRight(18);
        idfServiceView.setLeftImage(drawableToBitmap(Objects.requireNonNull(ContextCompat.getDrawable(mContext, R.mipmap.setting_icon))));
        idfServiceView.setLeftText("识别服务器IP");
        idfServiceView.setLeftTextSize(16);
        idfServiceView.setLeftTextColor(Color.BLACK);
        idfServiceView.setRightText(IdfConst.getInstance().getIdfServiceUrl());
        idfServiceView.setRightImage(drawableToBitmap(Objects.requireNonNull(ContextCompat.getDrawable(mContext, R.mipmap.enter_icon))));
        idfServiceView.setOnClickListener(view -> {
            InputDialog inputDialog = new InputDialog(mContext);
            inputDialog.show();
            inputDialog.getMsgTv().setText("请输入识别服务器IP");
            inputDialog.getEditText().setText(IdfConst.getInstance().getIdfServiceUrl());
            inputDialog.getOkTv().setOnClickListener(view1 -> {
                String row = inputDialog.getEditText().getText().toString();
                if (!"".equals(row)) {
                    IdfConst.getInstance().setIdfServiceUrl(row);
                    inputDialog.dismiss();
                    idfServiceView.setRightText(IdfConst.getInstance().getIdfServiceUrl());

                } else {
                    Toast.makeText(mContext, "输入有误，请重新输入", Toast.LENGTH_LONG).show();
                }
            });
        });
        mainLinear.addView(idfServiceView);

        line = new View(mContext);
        line.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1));
        line.setBackgroundColor(Color.rgb(224, 224, 224));
        mainLinear.addView(line);


        OptionItemView idfSystemView = new OptionItemView(mContext);
        idfSystemView.setPadding(20, 20, 20, 20);
        idfSystemView.setLayoutParams(linearParams);
        selector = new StateListDrawable();
        selector.addState(new int[]{android.R.attr.state_pressed}, ContextCompat.getDrawable(mContext,R.color.gray8));
        selector.addState(new int[]{android.R.attr.state_selected}, ContextCompat.getDrawable(mContext,R.color.white));
        selector.addState(new int[]{}, ContextCompat.getDrawable(mContext,R.color.white));
        idfSystemView.setBackground(selector);
        idfSystemView.setLeftImageMarginLeft(20);
        idfSystemView.setLeftImageMarginRight(18);
        idfSystemView.setLeftImage(drawableToBitmap(Objects.requireNonNull(ContextCompat.getDrawable(mContext, R.mipmap.setting_icon))));
        idfSystemView.setLeftText("数据服务器IP");
        idfSystemView.setLeftTextSize(16);
        idfSystemView.setLeftTextColor(Color.BLACK);
        idfSystemView.setRightText(IdfConst.getInstance().getIdfSystemUrl());
        idfSystemView.setRightImage(drawableToBitmap(Objects.requireNonNull(ContextCompat.getDrawable(mContext, R.mipmap.enter_icon))));
        idfSystemView.setOnClickListener(view -> {
            InputDialog inputDialog = new InputDialog(mContext);
            inputDialog.show();
            inputDialog.getMsgTv().setText("请输入数据服务器IP");
            inputDialog.getEditText().setText(IdfConst.getInstance().getIdfSystemUrl());
            inputDialog.getOkTv().setOnClickListener(view1 -> {
                String row = inputDialog.getEditText().getText().toString();
                if (!"".equals(row)) {
                    IdfConst.getInstance().setIdfSystemUrl(row);
                    inputDialog.dismiss();
                    idfSystemView.setRightText(IdfConst.getInstance().getIdfSystemUrl());

                } else {
                    Toast.makeText(mContext, "输入有误，请重新输入", Toast.LENGTH_LONG).show();
                }
            });
        });
        mainLinear.addView(idfSystemView);
        return scrollView;
    }


    public static Bitmap drawableToBitmap(Drawable drawable) {
        // 取 drawable 的长宽
        int w = drawable.getIntrinsicWidth();
        int h = drawable.getIntrinsicHeight();

        // 取 drawable 的颜色格式
        Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                : Bitmap.Config.RGB_565;
        // 建立对应 bitmap
        Bitmap bitmap = Bitmap.createBitmap(w, h, config);
        // 建立对应 bitmap 的画布
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, w, h);
        // 把 drawable 内容画到画布中
        drawable.draw(canvas);
        return bitmap;
    }

    private void init() {
        String registrationNumber = SdcardUtils.getRegistrationNumber();

        if (StringUtils.isEmpty(registrationNumber)) {
            regView.setRightText("未激活");
        } else {
            regView.setRightText("已激活");
        }
    }
}
